package com.qiku.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiku.dao.PromotionAdMapper;
import com.qiku.domin.PromotionAd;
import com.qiku.domin.PromotionAdVO;
import com.qiku.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PromotionAdServiceImpl implements PromotionAdService {

    @Autowired
    private PromotionAdMapper promotionAdMapper;

    @Override
    public void savePromotionAd(PromotionAd promotionAd) {

        Date date = new Date();
        promotionAd.setCreateTime(date);
        promotionAd.setUpdateTime(date);
//        promotion.setEndTime(date);
//        promotion.setEndTime(date);
        promotionAdMapper.savePromotionAd(promotionAd);
    }

    @Override
    public void updatePromotionAd(PromotionAd promotionAd) {
        promotionAd.setUpdateTime(new Date());
        promotionAdMapper.updatePromotionAd(promotionAd);
    }

    @Override
    public PromotionAd findPromotionAdById(Integer id) {
        return promotionAdMapper.findPromotionAdById(id);
    }

    @Override
    public void updatePromotionAdStatus(Integer id , int status) {
        PromotionAd promotionAd = new PromotionAd();
        promotionAd.setUpdateTime(new Date());
        promotionAd.setId(id);
        promotionAd.setStatus(status);
        promotionAdMapper.updatePromotionAdStatus(promotionAd);
    }

    @Override
    public PageInfo<PromotionAd> findAllPromotionAdByPage(PromotionAdVO promotionAdVO) {
        // 分页处理  设置分页相关的属性            当前页是第几页            该页要显示多少条数据
        PageHelper.startPage(promotionAdVO.getcurrentPage() , promotionAdVO.getPageSize());
        // 获取广告列表
        List<PromotionAd> promotionAdList = promotionAdMapper.findAllPromotionAdByPage();
        // 根据分页参数 进行分页
        PageInfo<PromotionAd> adPageInfo = new PageInfo<>(promotionAdList);
        return adPageInfo;

    }
}
